﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//测试能不能提交
using System.Data.SqlClient;
namespace XT.Exam.Common
{
   public  class CommonHelp
    { 
        #region///添加
       public static string ClassToSQL<T>(T t, string tableName, Dictionary<string, bool> PKName, params SqlParameter[] parameter) where T : class
        {
            string SQL = "INSERT INTO {0} ({1}) VALUES ({2})";

            string filed = "";
            string value = "";

            string Pk = PKName.Keys.First();
            bool ifPK = PKName.Values.First();

            foreach (var item in t.GetType().GetProperties())
            {
                foreach (var i in parameter)
                {
                    if ("@" + item.Name == i.ParameterName)
                    {
                        filed += item.Name + ",";
                        if (item.PropertyType == typeof(string))
                        {
                            value += "'"+i.Value + "'  ,";
                        }
                        else
                        {
                            if (item.PropertyType == typeof(DateTime))
                            {
                                value += "'" + i.Value + "' , ";
                            }
                            else
                            {
                                if (item.PropertyType == typeof(int) || item.PropertyType == typeof(decimal))
                                {
                                    value +=  "" + i.Value + " ,";
                                }
                            }
                        }
                    }
                }
                //if (ifPK)
                //{
                //    if (item.Name == Pk)
                //        continue;
                //}
                //filed += item.Name + ",";
                //if (item.PropertyType == typeof(string) || item.PropertyType == typeof(DateTime))
                //{
                //    value += "'" + item.GetValue(t) + "',";
                //}
                //else if (item.PropertyType == typeof(int) || item.PropertyType == typeof(decimal))
                //{
                //    value += item.GetValue(t) + ",";
                //}
            }

            filed = filed.TrimEnd(',');
            value = value.TrimEnd(',');

            return string.Format(SQL, tableName, filed, value);
        }
        #endregion
        #region//删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public static string ClassTODel<T>(T t, string tableName, Dictionary<string, bool> PkName, params SqlParameter[] parameter) where T : class
        {

            string sql = "delete from {0} where {1}";
            string PK = PkName.Keys.First();
            string where = "";
            var pkitem = t.GetType().GetProperty(PK);
            if (pkitem.PropertyType == typeof(string) || pkitem.PropertyType == typeof(DateTime))
            {
                where = PK + "='" + pkitem.GetValue(t) + "'";
            }
            else
            {
                where = PK + "=" + pkitem.GetValue(t);
            }


            return string.Format(sql, tableName, where);
        }
        #endregion

        #region///修改
        public static string ClassToUpdate<T>(T t, string tableName, Dictionary<string, bool> PKname) where T : class
        {
            string sql = "update {0} set {1} where {2}";
            string value = "";
            string where = "";
            string Pk = PKname.Keys.First();
            bool pkv = PKname.Values.First();
            foreach (var item in t.GetType().GetProperties())
            {
                if (pkv)
                {
                    if (item.Name == Pk)
                    {
                        continue;
                    }
                }
                if (item.PropertyType == typeof(string) || item.PropertyType == typeof(DateTime))
                {
                    value += item.Name + "='" + item.GetValue(t) + "',";
                }
                else if (item.PropertyType == typeof(int) || item.PropertyType == typeof(decimal))
                {
                    value += item.Name + "=" + item.GetValue(t) + ",";
                }
            }
            value = value.TrimEnd(',');
            var Pkitem = t.GetType().GetProperty(Pk);
            if (Pkitem.PropertyType == typeof(string) || Pkitem.PropertyType == typeof(DateTime))
                where = Pk + "= '" + Pkitem.GetValue(t) + "'";
            else
                where = Pk + "=" + Pkitem.GetValue(t);
            return string.Format(sql, tableName, value, where);
        }
        #endregion
        #region ///查询，返填，显示
        public static string ClassToShow<T>(string tableName, params SqlParameter[] parmeter)
        {
            string sql = "select * from {0}";

            if (parmeter.Length > 0)
            {

                Type t = typeof(T);

                T t1 = (T)t.Assembly.CreateInstance(t.FullName);
                string value = "";
                foreach (var item in t1.GetType().GetProperties())
                {
                    foreach (var i in parmeter)
                    {
                        if ("@" + item.Name == i.ParameterName)
                        {
                            if (item.PropertyType == typeof(string))
                            {
                                value += item.Name + " like '" + i.Value + "' and ";
                            }
                            else
                            {
                                if (item.PropertyType == typeof(DateTime))
                                {
                                    value += item.Name + " = '" + i.Value + "' and ";
                                }
                                else
                                {
                                    if (item.PropertyType == typeof(int) || item.PropertyType == typeof(decimal))
                                    {
                                        value += item.Name + " = " + i.Value + " and ";
                                    }
                                }
                            }
                        }
                    }
                }
                tableName += " where " + value.Substring(0, value.Length - 4);
                return string.Format(sql, tableName);
            }

            return string.Format(sql, tableName);

        }
        #endregion
    }
}
